<template>
  <div>
    <el-dialog
      :title="info.isAdd ? '角色添加' : '角色编辑'"
      :visible.sync="info.isshow"
      @closed="closed"
    >
      {{ user }}<br>{{attrArr}}
      <el-form :model="user">
        <el-form-item label="规格名称" label-width="120px">
          <el-input v-model="user.specsname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="规格属性"
          label-width="120px"
          v-for="(item, index) in attrArr"
          :key="index"
        >
          <div class="line">
            <el-input v-model="item.val" autocomplete="off"></el-input>
            <el-button type="primary" v-if="index == 0" @click="attrArr.push({val:''})">新增规格属性</el-button>
            <el-button type="danger" v-else @click="attrArr.splice(index,1)">删除</el-button>
          </div>
        </el-form-item>
        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="user.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="add" v-if="this.info.isAdd"
          >添 加</el-button
        >
        <el-button type="primary" @click="update" v-else>修 改</el-button>
      </div>
    </el-dialog>
    
  </div>
</template>

<script>
import { mapGetters, mapActions } from "vuex";
import { specsadd, specsedit, specsinfo } from "../../../http/api";
import { successAlert } from "../../../utils/alert";
export default {
  props: ["info"],
  data() {
    return {
      user: {
        specsname: "",
        attrs: "",
        status: 1,
      },
      attrArr: [{ val: "" }, { val: "" }],
    };
  },
  computed: {
    ...mapGetters({
      userInfo: "userInfo",
      list:"specs/list",
      total: "specs/total",
      size: "specs/size",
    }),
  },
  methods: {
    ...mapActions({
      changeUserInfo: "changeUserInfo",
      reqList:"specs/reqList",
      reqListTwo:"specs/reqListTwo"
    }),

    cancel() {
      this.info.isshow = false;
    },
    add() {
      this.user.attrs=(this.attrArr.map(item=>item.val)).join(",");
      specsadd(this.user).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.info.isshow = false;
          this.empty();
          this.reqListTwo();
        }
      });
    },
    update() {
      this.user.attrs=(this.attrArr.map(item=>item.val)).join(",");
      specsedit(this.user).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          //判断当前修改的user.id是不是当前登录用户的roleid
          //如果是就自动退出，如果不是，什么都不做
          this.info.isshow = false;
          this.empty();
          this.reqListTwo();
        }
      });
    },
    empty() {
      this.user = {
        specsname: "",
        attrs: "",
        status: 1,
      };
      this.attrArr = [{ val: "" }, { val: "" }];
    },
    getOne(id) {
      specsinfo({ id }).then((res) => {
        if (res.data.code == 200) {
          this.attrArr=res.data.list[0].attrs.map(item=>({val:item}));
          this.user = res.data.list[0];
          this.user.id = id;
        }
      });
    },

    //如果是编辑窗口取消，清空数据，添加窗口的取消，不需要清空
    closed() {
      if (!this.info.isAdd) {
        this.empty();
      }
    },
  },
  mounted() {
    
  },
};
</script>

<style lang="less" scoped>
@import "../../../less/index.less";
.line {
  display: flex;
  .el-input {
    flex: 1;
  }
}
</style>